#pragma once
#include "Common.hpp"
#include "Fixed_LengthPool.hpp"
#include "PageMap.hpp"

class PageCache
{
public:
    static PageCache* GetInstance()
    {
        return &_inst;
    }
    //获取Span定长内存池对象
    FLPool<Span>& GetSpanPool() { return _spanPoll;}
    // 获取锁
    pthread_mutex_t& GetMtx() { return _pageMtx; }
    // 内存块地址转换为Span地址
    Span* MapAddressToSpan(void* obj);
    //给中心缓存一个Span
    Span* FetchFromPageCache(int k);
    // 回收中心缓存中的Span函数
    void ReleaseSpanToRageCache(Span* span);
private:
    SpanList _pc_spanlists[NPAGE];
    pthread_mutex_t _pageMtx;

    // //页号和Span的映射关系
    // std::unordered_map<PAGE_ID, Span*> _idMapToSpan;
    //基数树代替unordered_map
    TCMalloc_PageMap1<32 - PAGE_SHIFT> _idMapToSpan;
    //定长内存池
    FLPool<Span> _spanPoll;

    // 单例模式
    static PageCache _inst;

    PageCache(){ pthread_mutex_init(&_pageMtx, nullptr); }
    PageCache(const PageCache&) = delete;
    PageCache& operator=(const PageCache&) = delete;
};